﻿
namespace MVVM_S.Samples.HelpDesk.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using MVVM_S.Samples.HelpDesk.Web.Models;


    // Implements application logic using the HelpdeskEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class HelpdeskService : LinqToEntitiesDomainService<HelpdeskEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Categories' query.
        public IQueryable<Category> GetCategories()
        {
            return this.ObjectContext.Categories;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Notes' query.
        public IQueryable<Note> GetNotes()
        {
            return this.ObjectContext.Notes;
        }

        public void InsertNote(Note note)
        {
            if ((note.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(note, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Notes.AddObject(note);
            }
        }

        public void UpdateNote(Note currentNote)
        {
            this.ObjectContext.Notes.AttachAsModified(currentNote, this.ChangeSet.GetOriginal(currentNote));
        }

        // Do not allow Deletion
        //public void DeleteNote(Note note)
        //{
        //    if ((note.EntityState == EntityState.Detached))
        //    {
        //        this.ObjectContext.Notes.Attach(note);
        //    }
        //    this.ObjectContext.Notes.DeleteObject(note);
        //}

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Offices' query.
        public IQueryable<Office> GetOffices()
        {
            return this.ObjectContext.Offices;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Tickets' query.
        public IQueryable<Ticket> GetTickets()
        {
            return this.ObjectContext.Tickets
                .Include("Notes");
        }

        public void InsertTicket(Ticket ticket)
        {
            if ((ticket.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(ticket, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Tickets.AddObject(ticket);
            }
        }

        public void UpdateTicket(Ticket currentTicket)
        {
            this.ObjectContext.Tickets.AttachAsModified(currentTicket, this.ChangeSet.GetOriginal(currentTicket));
        }

        // Do not allow Deletion
        //public void DeleteTicket(Ticket ticket)
        //{
        //    if ((ticket.EntityState == EntityState.Detached))
        //    {
        //        this.ObjectContext.Tickets.Attach(ticket);
        //    }
        //    this.ObjectContext.Tickets.DeleteObject(ticket);
        //}

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Users' query.
        public IQueryable<User> GetUsers()
        {
            return this.ObjectContext.Users;
        }
    }
}


